<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:gen="dummy-namespace-for-the-generated-xslt"
	exclude-result-prefixes="xsl">

	<xsl:namespace-alias stylesheet-prefix="gen"
		result-prefix="xsl" />

	<xsl:template match="rule">

		<gen:stylesheet version="1.0">

			<gen:output method="xml" version="1.0" encoding="UTF-8"
				indent="yes" />
			<gen:template priority="1" name="main" match="/">
				<data>
					<gen:for-each select="data/packet[@dismissed='false' or not(@dismissed)]">
						<gen:choose>

							<xsl:for-each select="filter">
							<gen:when>
								<xsl:attribute name="test">
									<xsl:choose>
										
										<xsl:when test="fieldMethod='contains'">
                                			<xsl:value-of
												select='concat("field[@name=&#39;", fieldName, "&#39; and contains(@show,&#39;",expression,"&#39;)]")' />
										</xsl:when>
										<xsl:when test="fieldMethod='not contains'">
                                			<xsl:value-of
												select='concat("field[@name=&#39;", fieldName, "&#39; and not(contains(@show,&#39;",expression,"&#39;))]")' />
										</xsl:when>
										
										<xsl:when test="fieldMethod='equals'">
                                			<xsl:value-of
												select='concat("field[@name=&#39;", fieldName, "&#39; and @show=&#39;",expression,"&#39;]")' />
										</xsl:when>
										<xsl:when test="fieldMethod='not equals'">
                                			<xsl:value-of
												select='concat("field[@name=&#39;", fieldName, "&#39; and not(@show=&#39;",expression,"&#39;)]")' />
										</xsl:when>
										
										<xsl:when test="fieldMethod='starts with'">
                                			<xsl:value-of
												select='concat("field[@name=&#39;", fieldName, "&#39; and starts-with(@show,&#39;",expression,"&#39;)]")' />
										</xsl:when>
										<xsl:when test="fieldMethod='not starts with'">
                                			<xsl:value-of
												select='concat("field[@name=&#39;", fieldName, "&#39; and not(starts-with(@show,&#39;",expression,"&#39;))]")' />
										</xsl:when>
										
										<xsl:when test="fieldMethod='ends with'">
                                			<xsl:value-of
												select='concat("field[@name=&#39;", fieldName, "&#39; and ends-with(@show,&#39;",expression,"&#39;)]")' />
										</xsl:when>
										<xsl:when test="fieldMethod='not ends with'">
                                			<xsl:value-of
												select='concat("field[@name=&#39;", fieldName, "&#39; and not(ends-with(@show,&#39;",expression,"&#39;))]")' />
										</xsl:when>
										
										<xsl:when test="fieldMethod='matches'">
                                			<xsl:value-of
												select='concat("field[@name=&#39;", fieldName, "&#39; and matches(@show,&#39;",expression,"&#39;)]")' />
										</xsl:when>
										<xsl:when test="fieldMethod='not starts with'">
                                			<xsl:value-of
												select='concat("field[@name=&#39;", fieldName, "&#39; and not(matches(@show,&#39;",expression,"&#39;))]")' />
										</xsl:when>
										
										<xsl:otherwise>
                                		<xsl:value-of
												select='concat("field[@name=&#39;", fieldName, "&#39; and compare(@show,&#39;",expression,"&#39;)", fieldMethod, "0]")' />
										</xsl:otherwise>
									</xsl:choose>
                            	</xsl:attribute>

								<xsl:choose>

									<xsl:when test="../filterType='include' or ../filterType='tagging'">
										<gen:call-template name="copy-valid" />
									</xsl:when>

									<xsl:otherwise>
										<gen:call-template name="copy-dismissed" />
									</xsl:otherwise>

								</xsl:choose>

							</gen:when>
							
							</xsl:for-each>
							
							<gen:otherwise>

								<xsl:choose>

									<xsl:when test="filterType='include' or filterType='tagging'">
										<gen:call-template name="copy-dismissed" />
									</xsl:when>

									<xsl:otherwise>
										<gen:call-template name="copy-valid" />
									</xsl:otherwise>

								</xsl:choose>

							</gen:otherwise>
						</gen:choose>
					</gen:for-each>
				</data>
			</gen:template>

			<xsl:choose>

				<xsl:when test="filterType='include' or filterType='tagging'">

					<gen:template priority="2" name="copy-valid" match="@*|node()">
						<gen:copy>
							<gen:if test="name()='packet'">
								<xsl:if test="filterType='include'">
									<gen:attribute name="dismissed">false</gen:attribute>
								</xsl:if>
								<tag>
									<gen:value-of select="tagName" />
								</tag>
							</gen:if>
							<gen:apply-templates select="@*|node()"></gen:apply-templates>
						</gen:copy>
					</gen:template>

					<gen:template priority="3" name="copy-dismissed"
						match="@*|node()">
						<gen:copy>
							<gen:apply-templates select="@*|node()"></gen:apply-templates>
						</gen:copy>
					</gen:template>


				</xsl:when>
				<xsl:otherwise>


					<gen:template priority="2" name="copy-dismissed"
						match="@*|node()">
						<gen:copy>
							<gen:attribute name="dismissed">true</gen:attribute>
							<tag>
								<gen:value-of select="tagName" />
							</tag>

							<gen:apply-templates select="@*|node()"></gen:apply-templates>
						</gen:copy>
					</gen:template>

					<gen:template priority="3" name="copy-valid" match="@*|node()">
						<gen:copy>
							<gen:apply-templates select="@*|node()"></gen:apply-templates>
						</gen:copy>
					</gen:template>


				</xsl:otherwise>

			</xsl:choose>

		</gen:stylesheet>


	</xsl:template>
</xsl:stylesheet>
